package org.funmodelling;

import com.sun.opengl.util.Animator;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;

/**
 * OGLCube.java <BR>
 * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
 *
 * This version is equal to Brian Paul's version 1.2 1999/10/21
 */
public class OGLCube implements GLEventListener, KeyListener, MouseListener, MouseWheelListener {

    GLU glu;
    private double x = 0, y = 0, z = -6;
    private double rx = 0, ry = 0, rz = 0;
    private double ex = 3, ey = 4, ez = 3;

    public static void main(String[] args) {
        Frame frame = new Frame("Simple JOGL Application");
        GLCanvas canvas = new GLCanvas();

        canvas.addGLEventListener(new OGLCube());
        frame.add(canvas);
        frame.setSize(640, 480);
        final Animator animator = new Animator(canvas);
        frame.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                // Run this on another thread than the AWT event queue to
                // make sure the call to Animator.stop() completes before
                // exiting
                new Thread(new Runnable() {

                    public void run() {
                        animator.stop();
                        System.exit(0);
                    }
                }).start();
            }
        });
        

        // Center frame
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setFocusable(true);


        animator.start();
    }

    public void init(GLAutoDrawable drawable) {
        // Use debug pipeline
        // drawable.setGL(new DebugGL(drawable.getGL()));

        GL gl = drawable.getGL();
        System.err.println("INIT GL IS: " + gl.getClass().getName());

        // Enable VSync
        gl.setSwapInterval(1);

        // Setup the drawing area and shading mode
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        gl.glShadeModel(GL.GL_SMOOTH); // try setting this to GL_FLAT and see what happens.
        //
        glu = new GLU();
        drawable.addKeyListener(this);
        drawable.addMouseListener(this);
        drawable.addMouseWheelListener(this);
    }

    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        if (height <= 0) { // avoid a divide by zero error!

            height = 1;
        }
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluPerspective(45.0f, h, 1.0, 20.0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        // Clear the drawing area
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();

        // Move the "drawing cursor" around
        gl.glTranslated(x, y, z);
        gl.glRotated(rx, 1, 0, 0);
        gl.glRotated(ry, 0, 1, 0);
        gl.glRotated(rz, 0, 0, 1);


        drawCube(gl, 1.0f);


        // Flush all drawing operations to the graphics card
        gl.glFlush();
    }

    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

    public void drawCube(GL gl, float a) {

        gl.glBegin(GL.GL_LINE_STRIP);
        gl.glColor3f(1.0f, 0.0f, 0.0f);
        gl.glVertex3f(-a, -a, a);
        gl.glVertex3f(-a, +a, a);
        gl.glVertex3f(+a, +a, a);
        gl.glVertex3f(+a, -a, a);
        gl.glVertex3f(-a, -a, a);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
        gl.glVertex3f(-a, -a, a);
        gl.glVertex3f(-a, -a, -a);
        gl.glColor3f(1.0f, 1.0f, 0.0f);
        gl.glVertex3f(-a, -a, -a);
        gl.glVertex3f(-a, +a, -a);
        gl.glVertex3f(+a, +a, -a);
        gl.glVertex3f(+a, -a, -a);
        gl.glVertex3f(-a, -a, -a);
        gl.glEnd();


        gl.glBegin(GL.GL_LINES);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
        gl.glVertex3f(-a, +a, +a);
        gl.glVertex3f(-a, +a, -a);
        gl.glVertex3f(+a, +a, +a);
        gl.glVertex3f(+a, +a, -a);
        gl.glColor3f(0.0f, 0.0f, 1.0f);
        gl.glVertex3f(+a, -a, +a);
        gl.glVertex3f(+a, -a, -a);
        gl.glEnd();
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseWheelMoved(MouseWheelEvent e) {
        int n = e.getWheelRotation();
        double inc = (double) n / 5.0;
        ex += inc;
        ey += inc;
        ez += inc;
    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {

        if (e.isControlDown() && (e.getKeyCode() == KeyEvent.VK_NUMPAD6)) {
            rx += 10;
            return;
        }
        if (e.isControlDown() && (e.getKeyCode() == KeyEvent.VK_NUMPAD4)) {
            rx -= 10;
            return;
        }
        if (e.isControlDown() && (e.getKeyCode() == KeyEvent.VK_NUMPAD8)) {
            ry += 10;
            return;
        }
        if (e.isControlDown() && (e.getKeyCode() == KeyEvent.VK_NUMPAD2)) {
            ry -= 10;
            return;
        }
        if (e.isControlDown() && (e.getKeyCode() == KeyEvent.VK_ADD)) {
            rz += 10;
            return;
        }
        if (e.isControlDown() && (e.getKeyCode() == KeyEvent.VK_SUBTRACT)) {
            rz -= 10;
            return;
        }


        if (e.getKeyCode() == KeyEvent.VK_NUMPAD6) {
            x += 1;
            return;
        }
        if (e.getKeyCode() == KeyEvent.VK_NUMPAD4) {
            x -= 1;
            return;
        }
        if (e.getKeyCode() == KeyEvent.VK_NUMPAD8) {
            y += 1;
            return;
        }
        if (e.getKeyCode() == KeyEvent.VK_NUMPAD2) {
            y -= 1;
            return;
        }
        if (e.getKeyCode() == KeyEvent.VK_ADD) {
            z += 1;
            return;
        }
        if (e.getKeyCode() == KeyEvent.VK_SUBTRACT) {
            z -= 1;
            return;
        }
        if (e.getKeyCode() == KeyEvent.VK_NUMPAD5) {
            x=y=0; z=-6; rx=ry=rz=0;
            return;
        }


    }

    public void keyReleased(KeyEvent e) {
    }
}

